<template>
  <div>
    <el-button type="success" @click="submit">批量取消</el-button>
    <el-button type="success" @click="refresh">刷新</el-button>
    <!-- 表单 -->
    <el-table
      ref="multipleTable"
      :data="Uncommitted"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" prop="needId" width="55">
      </el-table-column>
      <el-table-column prop="orderId" label="订单编号" width="120">
      </el-table-column>
      <el-table-column prop="typeName" label="订单类型" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="tranType" label="运输类型" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="username" label="客户名称" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="下单时间" width="100">
        <template slot-scope="scope">{{ scope.row.orderTime }}</template>
      </el-table-column>
      <el-table-column prop="route" label="路线" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="goodsName" label="货物类型" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="number" label="总数量" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="weight" label="总重量" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="bulk" label="总体积" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            plain
            @click="handleEdit(scope.$index, scope.row)"
            >取消订单</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import Need from "@/view/vehicle/need";

export default {
  name: "Submitted",
  props: ["search", "sta0"],
  data() {
    return {
      /* 分页 */
      currentPage: 1,
      pageSizes: [10, 20, 30],
      pageSize: 10,
      total: 0,

      /* 勾选框条数 */
      multipleSelection: '',
      /* 初始化数据对象 */
      Uncommitted: [],
    };
  },
  components: {
    Need,
  },
  /* 监听属性值的变化，执行对应方法 */
  watch: {
    "search.orders": {
      handler() {
        console.log(this.search.orders + "asdhkajsd");
        this.findAll(
          this.currentPage,
          this.pageSize,
          this.search.orders,
          this.search.usernames
        );
      },
    },
    "search.usernames": {
      handler() {
        console.log(this.search.usernames + "asdhkajsd");
        this.findAll(
          this.currentPage,
          this.pageSize,
          this.search.orders,
          this.search.usernames
        );
      },
    },
    sta0: {
      handler() {
        console.log(this.search.usernames + "222222222222");
        this.search.orders = null;
        this.search.usernames = null;
        this.findAll(
          this.currentPage,
          this.pageSize,
          this.search.orders,
          this.search.usernames
        );
      },
    },
  },
  methods: {

  /* 数据：批量取消订单表 */
    submit() {
      let a = this.currentPage;
      let b = this.pageSize;
      let ids = [];
      if (this.multipleSelection == "") {
        console.log("请选择需要提交的数据");
        alert("请选择需要提交的数据");
      } else {
        for (let i = 0; i < this.multipleSelection.length; i++) {
          let j = this.multipleSelection[i].needId;
          ids.push(j);
        }
        this.$confirm("此操作将提交该订单, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            var status = 2;
            console.log(ids);
            this.$http({
              method: "PUT",
              url: `http://localhost:8090/tNeed/${status}`,
              data: ids,
            }).then((resp) => {
              if (a > (this.total - 1) / b && (this.total - 1) % b == 0) {
                this.findAll(a - 1, b,this.search.orders,this.search.usernames);
                console.log(this.total);
                console.log(this.total / b);
                console.log(a);
                console.log(b);
              } else {
                this.findAll(a, b,this.search.orders,this.search.usernames);
              }
              if (resp.data.code == 1) {
                //2. 信息提示
                this.$message({
                  message: "" + resp.data.msg,
                  type: "success",
                });
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消提交",
            });
          });
      }
    },

    /* 多选数据赋值 */
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    /* 刷新数据 */
    refresh() {
      this.search.orders = null;
      this.search.usernames = null;
      this.findAll(
        this.currentPage,
        this.pageSize,
        this.search.orders,
        this.search.usernames
      );
    },

    /* 初始化数据 */
    findAll(currentPage, pageSize, orderIds, usernames) {
      /* 判断搜索框是否有值 */
      if (this.search.orders != null || this.search.usernames != null) {
        orderIds = this.search.orders;
        usernames = this.search.usernames;
        console.log(orderIds + "asd" + usernames);
      }
      var status = 1;
      /* asiox */
      this.$http({
        method: "GET",
        url: "http://localhost:8090/tNeed/Uncommitted",
        params: {
          currentPage: currentPage,
          pageSize: pageSize,
          orderId: orderIds,
          username: usernames,
          status: status,
        },
      }).then((resp) => {
        console.log(resp);
        this.Uncommitted = resp.data.data.records;
        this.total = resp.data.data.total;
      });
    },

    /* 每一页条数改变的方法 */
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.findAll(
        this.currentPage,
        this.pageSize,
        this.orderIds,
        this.usernames
      );
    },

    /* 页码改变的方法 */
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.findAll(
        this.currentPage,
        this.pageSize,
        this.orderIds,
        this.usernames
      );
    },

    /* 数据提交:提交单个订单表 */
    handleEdit(index, row) {
      console.log(row);
      console.log("adsasdasfgqqwredfsafqewfsdfqaerfq");
      var needId = row.needId;
      let a = this.currentPage;
      this.$confirm("此操作将提交该订单, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var status = 2;
          this.$http({
            method: "PUT",
            url: `http://localhost:8090/tNeed`,
            params: {
              needId: needId,
              status: status,
            },
          }).then((resp) => {
            console.log(resp.data);
            if (resp.data.code === 100000) {
              this.$message({
                message: resp.data.msg,
                type: "success",
              });
            } else {
              this.$message.error(resp.data.msg);
            }
            if (
              a > (this.total - 1) / this.pageSize &&
              (this.total - 1) % this.pageSize == 0
            ) {
              console.log(this.total);
              this.findAll(
                a - 1,
                this.pageSize,
                this.search.orders,
                this.search.usernames
              );
            } else {
              this.findAll(
                a,
                this.pageSize,
                this.search.orders,
                this.search.usernames
              );
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消提交",
          });
        });
    },
  },
  mounted() {},
  created() {
    this.findAll(1, 10,this.search.orders,this.search.usernames);
  },
};
</script>

<style>
</style>
